package lihao2;

public class Test5 {
    public static void main(String[] args) {
        String str = "dffcbargertgrcbaercba";
        String key = "cba";
        int count = getKeyStringCount(str,key);
        System.out.println("count="+count);

    }

    public static int getKeyStringCount(String str, String key) {
        //定义计数器记录出现位置
        int count = 0;
        if (!str.contains(key)){
            return count;
        }
        //记录子字符串出现的位置
        int index = 0;
        while ((index = str.indexOf(key)) != -1){
            str = str.substring(index + key.length());
            count++;
        }
        return count;
    }

}

